package org.freedoit.server.dao;

// Generated Oct 11, 2012 3:54:06 PM by Hibernate Tools 3.4.0.CR1

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import org.freedoit.server.om.Project;
import org.freedoit.shared.exception.ServerException;

/**
 * Home object for domain model class Project.
 * @see org.freedoit.om.Project
 * @author Hibernate Tools
 */
public class ProjectDAO {

	//private static final Log log = LogFactory.getLog(ProjectDAO.class);

	private final PersistenceManagerFactory pmFactory = DAOFactory.getPersistenceManagerFactory();

	public void persist(Project project) {
		//log.debug("persisting Project instance");
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			pm.makePersistent(project);
			//log.debug("persist successful");
		} catch (RuntimeException re) {
			//log.error("persist failed", re);
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}

	public void delete(Project project) {
		//log.debug("deleting Project instance");
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			pm.currentTransaction().begin();
			// We don't have a reference to the selected Product.
			// So we have to look it up first,
			project = pm.getObjectById(Project.class, project.getProjectId());
			pm.deletePersistent(project);

			pm.currentTransaction().commit();
			//log.debug("delete successful");
		} catch (RuntimeException re) {
			//log.error("delete failed", re);
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}

	public void update(Project project) {
		//log.debug("updating Project instance");
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			pm.currentTransaction().begin();
			pm.makePersistent(project);
			pm.currentTransaction().commit();
			//log.debug("update successful");
		} catch (RuntimeException re) {
			//log.error("update failed", re);
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}

	public Project findById(Long id) {
		//log.debug("getting Project instance with id: " + id);
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			Project instance = pm.getObjectById(Project.class, id);
			if (instance == null) {
				//log.debug("get successful, no instance found");
			} else {
				//log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			//log.error("get failed", re);
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Project> findByUserId(Long userId) {
		//log.debug("finding Project instances by user id:" + userId);
		PersistenceManager pm = pmFactory.getPersistenceManager();
		try {
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("select from ").append(Project.class.getName());
			sbQuery.append(" where userId == ").append(userId);
			List<Project> results = (List<Project>) pm.newQuery(sbQuery.toString()).execute();
			
//			int iSize = 0;
//			if (results != null) {
//				iSize = results.size();
//			}
			//log.debug("find by user id successful, result size: " + iSize);
			return results;
		} catch (RuntimeException re) {
			//log.error("find by user id failed", re);
			throw new ServerException(re);
		} finally {
			pm.close();
		}
	}
}
